﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace HousingDistribution
{
    public partial class FormPersonSearch : Form
    {
        public FormPersonSearch(FormPersonDetailsNoHousingTab parent, PersonTypeEnum type, string nameChr)
        {
            InitializeComponent();

            InitDGVPersonSearch();

            this.parent = parent;
            this.type = type;
            this.nameChr = nameChr;

            this.dataGridViewPersonSearch.Dock = DockStyle.Fill;
            this.dataGridViewPersonSearch.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;

            this.buttonClose.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        }
        
        private void FormPersonSearch_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(this.Query);
            t.Start();
        }

        #region DGV
        private void InitDGVPersonSearch()
        {
            DGVInit.Init(ref this.dataGridViewPersonSearch, DataDictionary.Instance().GetDGVConf(DataDictionary.Instance().dgvNames[1]));
        }

        int rowIndex = 0;
        private void dataGridViewPersonSearch_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //int rowIndex = e.RowIndex;
            rowIndex = e.RowIndex;

            //ReadDataAndDisplayDetails(rowIndex);
        }

        private void dataGridViewPersonSearch_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //int rowIndex = e.RowIndex;
            rowIndex = e.RowIndex;

            ReadDataAndDisplayDetails(rowIndex);

            this.Close();
        }

        void ReadDataAndDisplayDetails(int row)
        {
            if (row < 0)
                return;

            DataTable dt = (DataTable)this.dataGridViewPersonSearch.DataSource;
            PersonInfo person = this.parser.ParsePerson(dt.Rows[row]);
            this.parent.SetSearchPersonFlag(true);
            this.parent.DisplayPerson(person);
        }

        #endregion

        #region 查询数据并显示
        private PersonTypeEnum type;
        private string nameChr;
        private int MAX_COUNT = 100;
        private static string PERSON_QUERY_SQL_HEADER = "select * from person where ";

        void Query()
        {
            StringBuilder sql = new StringBuilder(PERSON_QUERY_SQL_HEADER);
            sql.Append("person.nameInitial like '");
            sql.Append(nameChr.ToUpper());
            sql.Append("%' ");
            sql.Append(" or person.mateNameInitial like '");
            sql.Append(nameChr.ToUpper());
            sql.Append("%' ");
            sql.Append("order by idperson desc limit 0, ");
            sql.Append(MAX_COUNT.ToString());

            DataTable dt = DBProc.Instance().ReadData(sql.ToString()).Tables[0];

            FormatDataTable(ref dt);
            
            BindDGVData(dt);
        }

        //格式化DataTable，增加人员类别名称列，将人员的类别转换成中文名称
        void FormatDataTable(ref DataTable dt)
        {
            //增加显示人员类型的列
            dt.Columns.Add(DGV_TYPE_NAME_COL, typeof(System.String));

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i][DGV_TYPE_NAME_COL] = DataDictionary.Instance().GetPersonType((PersonTypeEnum)dt.Rows[i][PERSON_TYPE_COL]);
            }
        }

        private delegate void BindDGVDataDelegate(DataTable dt);
        void BindDGVData(DataTable dt)
        {
            if (this.dataGridViewPersonSearch.InvokeRequired)
            {
                BindDGVDataDelegate d = new BindDGVDataDelegate(this.BindDGVData);
                this.Invoke(d, new object[] { dt });
            }
            else
            {
                this.dataGridViewPersonSearch.DataSource = dt;
            }
        }
        #endregion

        private void buttonOK_Click(object sender, EventArgs e)
        {
            ReadDataAndDisplayDetails(rowIndex);
            this.Close();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        FormPersonDetailsNoHousingTab parent;

        PersonParserFromMySql parser = new PersonParserFromMySql();

        String PERSON_TYPE_COL = DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TYPE);
        String DGV_TYPE_NAME_COL = DataDictionary.Instance().DGVNewColName(DGVNewColConsts.PERSON_TYPE_NAME);

    }
}
